#include "ElevatorIPXml.h"
#include "File/Ini/DeviceIni.h"
#include <qdebug.h>

ElevatorIPXml::ElevatorIPXml()
{
    m_mantissa="00";
}

bool ElevatorIPXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "Elevator")   //解析NET表
        {
            QDomNode netChild = rootChild.toElement().firstChild();
            ElevatorStruct elevatorStruct;
            while(!netChild.isNull())
            {
                if (netChild.toElement().tagName() == "Brand")
                {
                    QString brand = netChild.toElement().text();
                    setBrand(brand);
                }
                if (netChild.toElement().tagName() == "IP")  //解析IP
                {
                    QString ip = netChild.toElement().text();
                    elevatorStruct.setIP(ip);
                    qDebug()<<ip;
                }
                if(netChild.toElement().tagName() == "Floornum")
                {
                    QString FloorNum = netChild.toElement().text();
                    if(FloorNum.at(0)=='-')
                    {

                         QString data=FloorNum.right(FloorNum.size()-1);
                         quint8 floordata=256-(data.toLong(0,10));
                         elevatorStruct.setFlootNum(floordata);
                         qDebug()<<floordata;

                    }
                    else
                    {
                          QString data=FloorNum.left(FloorNum.size());
                          quint8 floordata=data.toLong(0,10);
                          elevatorStruct.setFlootNum(floordata);
                          qDebug()<<floordata;
                    }
                }
                if(netChild.toElement().tagName() == "UnitZone")
                {
                    setUnitZone(netChild.toElement().text());
                }
                if(netChild.toElement().tagName() == "Mantissa")
                {
                    m_mantissa = netChild.toElement().text();
                }
                if(netChild.toElement().tagName() == "RoomDir")
                {
                    m_roomDir = netChild.toElement().text();
                }
                if(netChild.toElement().tagName() == "F1Dir")
                {
                    m_f1Dir = netChild.toElement().text();
                }
                if(netChild.toElement().tagName() == "FB1Dir")
                {
                    m_fB1Dir = netChild.toElement().text();
                }
                if(netChild.toElement().tagName() == "FB2Dir")
                {
                    m_fB2Dir = netChild.toElement().text();
                }
                if(netChild.toElement().tagName() == "FB3Dir")
                {
                    m_fB3Dir = netChild.toElement().text();
                }
                if(netChild.toElement().tagName() == "Mantissa01Dir")
                {
                    m_M01Dir = netChild.toElement().text();
                }
                if(netChild.toElement().tagName() == "Mantissa02Dir")
                {
                    m_M02Dir = netChild.toElement().text();
                }
                if(netChild.toElement().tagName() == "Mantissa03Dir")
                {
                    m_M03Dir = netChild.toElement().text();
                }
                if(netChild.toElement().tagName() == "Mantissa04Dir")
                {
                    m_M04Dir = netChild.toElement().text();
                }
                netChild = netChild.nextSibling();
            }
            m_StructList.append(elevatorStruct);
        }
        rootChild = rootChild.nextSibling();
    }
    if(m_StructList.count() > 0)
        {
            DeviceIni *deviceIni = DeviceIni::getInstance();
            QString zone = deviceIni->getZoneByIP(m_StructList[0].getIP());
            setDoorZone(zone);

            qDebug()<<"ElevatorIPXml:door  Zone "<<getDoorZone();
        }
    return true;

}

bool ElevatorIPXml::clear()
{
    return false;
}

QList<ElevatorStruct>  ElevatorIPXml::StructList() const
{
    return m_StructList;
}

void ElevatorIPXml::setUnitZone(QString zone)
{
       m_unitZone = zone;
}
 QString ElevatorIPXml::getUnitZone()
 {
    return m_unitZone;
 }


void ElevatorIPXml::setDoorZone(QString zone)
{
    m_doorZone = zone;
}
QString ElevatorIPXml::getDoorZone()
{
    return m_doorZone;
}

void ElevatorIPXml::setBrand(QString brand)
{
    m_brand = brand;
}
QString ElevatorIPXml::getBrand()
{
    return m_brand;

}
QString ElevatorIPXml::getMantissa()
{
    return m_mantissa;
}

QString ElevatorIPXml::getRoomDir()
{
    return m_roomDir;
}
QString ElevatorIPXml::getF1Dir()
{
    return m_f1Dir;
}
QString ElevatorIPXml::getFB1Dir()
{
    return m_fB1Dir;
}
QString ElevatorIPXml::getFB2Dir()
{
    return m_fB2Dir;
}
QString ElevatorIPXml::getFB3Dir()
{
    return m_fB3Dir;
}

QString ElevatorIPXml::getM01Dir()
{
    return m_M01Dir;
}
QString ElevatorIPXml::getM02Dir()
{
    return m_M02Dir;
}
QString ElevatorIPXml::getM03Dir()
{
    return m_M03Dir;
}
QString ElevatorIPXml::getM04Dir()
{
    return m_M04Dir;
}

